using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class OrderItemController : ControllerBase
    {
        private readonly IOrderItemService _orderItemService;

        public OrderItemController(IOrderItemService orderItemService)
        {
            _orderItemService = orderItemService;
        }

        [HttpGet]
        public async Task<ActionResult<List<OrderItem>>> GetAllOrderItems()
        {
            var items = await _orderItemService.GetAllAsync();
            return Ok(items);
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<OrderItem>> GetOrderItemById(Guid id)
        {
            var item = await _orderItemService.GetByIdAsync(id);
            if (item == null)
                return NotFound();

            return Ok(item);
        }

        [HttpGet("order/{orderId}")]
        public async Task<ActionResult<List<OrderItem>>> GetOrderItemsByOrderId(Guid orderId)
        {
            var items = await _orderItemService.GetOrderItemsByOrderIdAsync(orderId);
            return Ok(items);
        }

        [HttpGet("product/{productId}")]
        public async Task<ActionResult<List<OrderItem>>> GetOrderItemsByProductId(Guid productId)
        {
            var items = await _orderItemService.GetOrderItemsByProductIdAsync(productId);
            return Ok(items);
        }

        [HttpGet("{orderItemId}/total")]
        public async Task<ActionResult<decimal>> GetOrderItemTotal(Guid orderItemId)
        {
            try
            {
                var total = await _orderItemService.CalculateItemTotalAsync(orderItemId);
                return Ok(total);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpPost]
        public async Task<ActionResult<OrderItem>> CreateOrderItem(OrderItem orderItem)
        {
            try
            {
                var createdItem = await _orderItemService.CreateAsync(orderItem);
                return CreatedAtAction(nameof(GetOrderItemById), new { id = createdItem.OrderItemId }, createdItem);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpPut("{id}")]
        public async Task<ActionResult<OrderItem>> UpdateOrderItem(Guid id, OrderItem orderItem)
        {
            if (id != orderItem.OrderItemId)
                return BadRequest();

            try
            {
                var updatedItem = await _orderItemService.UpdateAsync(orderItem);
                return Ok(updatedItem);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpPut("{id}/quantity/{quantity}")]
        public async Task<ActionResult<OrderItem>> UpdateQuantity(Guid id, int quantity)
        {
            try
            {
                var updatedItem = await _orderItemService.UpdateQuantityAsync(id, quantity);
                return Ok(updatedItem);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult> DeleteOrderItem(Guid id)
        {
            var result = await _orderItemService.DeleteAsync(id);
            if (!result)
                return NotFound();

            return NoContent();
        }
    }
} 